/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fsktms;

import java.net.Inet4Address;
import java.util.*;


public class Message implements IMessage
{

    static public int PACKAGE_DATA_SIZE = 8180;
    
    private boolean isCompleted = false;
    private ArrayList<byte[]> datas = new ArrayList<>();
    private Queue<byte[]> dataSent = new ArrayDeque<>();
    private byte[] messageData;
    private String hostAddress;
    private String destAddress;
    private int magicKey;
    
    public Message(String destAdd, byte[] data, int magicKey)
    {
        this.destAddress = destAdd;
        try
        {
            this.hostAddress = Inet4Address.getLocalHost().getHostAddress();
        }
        catch (Exception e)
        {
            System.err.println(e);
        }
        
        this.messageData = data;
        this.magicKey = magicKey;
    }
    
    public Message(String hostAdd, int magicKey)
    {
        this.hostAddress = hostAdd;
        try
        {
            this.destAddress = Inet4Address.getLocalHost().getHostAddress();
        }
        catch (Exception e)
        {
            System.err.println(e);
        }
        
        this.magicKey = magicKey;
    }
    
    public void appenData(byte[] data)
    {
        datas.add(data);
    }
    
    public void join()
    {
        int size = 0;
        for(byte[] smallData : datas)
        {
            size += smallData.length;
        }
        
        this.messageData = new byte[size];
        
        int pos = 0;
        for(byte[] smallData : datas)
        {
            System.arraycopy(smallData, 0, messageData, pos, smallData.length);
            pos += smallData.length;
        }
        
        this.isCompleted = true;
    }
    
    public void split()
    {
        int currentPos = 0;
        while(currentPos + PACKAGE_DATA_SIZE <= messageData.length)
        {
            byte[] packageData = new byte[PACKAGE_DATA_SIZE];
            System.arraycopy(messageData, currentPos, packageData, 0, PACKAGE_DATA_SIZE);
            this.dataSent.add(packageData);
            currentPos += PACKAGE_DATA_SIZE;
        }
        
        if (currentPos < messageData.length && currentPos + PACKAGE_DATA_SIZE > messageData.length)
        {
            int size = messageData.length % PACKAGE_DATA_SIZE;
            byte[] packageData = new byte[size];
            System.arraycopy(messageData, currentPos, packageData, 0, size);
            this.dataSent.add(packageData);
            currentPos += size;
        }
    }
    
    @Override
    public byte[] getData()
    {
        return this.messageData;
    }

    @Override
    public String getHostAddress() 
    {
        return this.hostAddress;
    }

    @Override
    public String getDestAddress()
    {
        return this.destAddress;
    }

    @Override
    public boolean isCompleted() {
        return this.isCompleted;
    }

    @Override
    public byte[] getNextDataPacket() {
        if(dataSent.size() > 0)
        {
            return dataSent.poll();
        }
        return null;
    }
    
    @Override
    public boolean isHasNextDataPacket(){
        return dataSent.size() > 0;
    }

    @Override
    public int getMagicKey() {
        return this.magicKey;
    }
}
